<template>
  <view class="my-disk">
    <uni-calendar
			:insert="true"
			:lunar="true"
			:start-date="startDate"
			:end-date="endDate"
			:selected="extraInfo"
			@change="change"
     />
  </view>
</template>

<script>
import {uniCalendar, uniPopup, uniPopupMessage, uniPopupDialog } from '@dcloudio/uni-ui'
import {getMyDisk} from '../../api/disk'
import {mapState} from 'vuex'
export default {
	name: 'my-disk',
	components: {
		uniCalendar,uniPopup, uniPopupMessage, uniPopupDialog
	},
	computed: {
		...mapState(['userInfo'])
	},
  data() {
		return {
			startDate: '2020-1-1',
			endDate: '2022-1-1',
			extraInfo: [],
			rawInfo: []
		}
	},
	methods: {
		getDiskInfo() {
			const data = {
				userId: this.userInfo.id
			}
			return getMyDisk(data).then(res => {
				console.log(res)
				if (res.code === 200) {
					this.rawInfo = res.data
					return res.data
				} else {
					return []
				}
			})
		},
		async handleRawData() {
			const data = await this.getDiskInfo()
			console.log('获取打卡信息')
			for(let i = 0; i < this.rawInfo.length; i++) {
				const obj = {
					"date": this.rawInfo[i].createTime,
					"info": '打卡',
					"data": {
						"id": this.rawInfo[i].id
					}
				}
				this.extraInfo.push(obj)
			}
			console.log(this.extraInfo)
		},
		change(obj) {
			// this.handleRawData()
			let diskId = undefined
			if(obj.extraInfo.data) {
				diskId = obj.extraInfo.data.id
			}
			const date = obj.fulldate
            const today = new Date()
            const year = today.getFullYear()
            const month = today.getMonth() + 1 >= 10 ? today.getMonth() + 1: '0'+today.getMonth() + 1
            const day = today.getDate() >= 10? today.getDate(): '0'+today.getDate()
            const myDate = year + '-' +  month + '-' + day
            console.log(date, myDate)
            if (diskId === undefined && date === myDate) {
				this.handleRawData()
				return uni.navigateTo({
					url: '/pages/mine/createDisk'
				})
			}
			else if (diskId === undefined) {
				return uni.showToast({
					title: '这一天没有打卡记录哦，只能记录当天的打卡~',
					duration: 2000,
					icon: 'none'
				})
			} else {
				uni.navigateTo({
					url: '/pages/mine/myDisk?date='+date+' 00:00:00'
				})
			}
		}
	},
	mounted() {
		this.handleRawData()
	},
	onLoad() {
		this.handleRawData()
	},
	onShow() {
		this.handleRawData()
	}
}
</script>

<style lang="scss">
.my-disk {
    width: 100%;
    height: 100%;
}
</style>